package com.jvm123.demo;

import com.jvm123.minio.service.MinioFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author yawn http://jvm123.com
 * 2020/1/15 17:17
 */
@RestController
public class TestController {

    @Autowired
    MinioFileService minioFileService;

    @GetMapping("test")
    public void get(HttpServletResponse response) throws IOException {
        // 存储文件
        minioFileService.save(new File("C:\\asd.txt"), "a.txt");

        // 获取文件
        File file = minioFileService.getFile("a.txt");

        // 获取输入流
        InputStream inputStream = minioFileService.getStream("a.txt");

        // 下载
        response.addHeader("Content-Disposition","attachment;filename=a.txt");
        ServletOutputStream os = response.getOutputStream();
        minioFileService.writeTo("a.txt", os);
    }

    @GetMapping("test2")
    public void test2(MultipartFile multipartFile, HttpServletResponse response) throws IOException {

        String bucketName = "demo-bucket";

        // 创建bucket
        boolean created = minioFileService.createBucket(bucketName);

        // 存储文件
        String saved = minioFileService.save(bucketName, new File("C:\\asd.txt"), "a.txt");

        // 删除文件
        boolean deleted = minioFileService.delete(bucketName, "a.txt");

        // 获取文件
        File file = minioFileService.getFile(bucketName, "a.txt");

        // 获取输入流
        InputStream inputStream = minioFileService.getStream(bucketName, "a.txt");

        // 下载
        response.addHeader("Content-Disposition","attachment;filename=a.txt");
        ServletOutputStream os = response.getOutputStream();
        minioFileService.writeTo(bucketName, "a.txt", os);
    }
}
